package com.beimihu.until;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Excel {
    public static void main(String[] args) {
        String filePath = "./InAppPurcharse.xlsx"; // Excel 文件的路径
        List<String> results = readAndConcatenate(filePath);

        // 输出结果
        for (String result : results) {
            System.out.println(result);
        }
    }

    /**
     * 读取 Excel 文件，并拼接“显示名称”和“描述”列的内容
     *
     * @param filePath Excel 文件路径
     * @return 拼接后的字符串列表
     */
    public static List<String> readAndConcatenate(String filePath) {
        List<String> concatenatedResults = new ArrayList<>();

        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

            // 遍历每一行（跳过第一行表头）
            for (int i = 1; i <= sheet.getLastRowNum() - 1; i++) {
                Row row = sheet.getRow(i);
                if (row != null) {
                    Cell productIdCell = row.getCell(4);
                    Cell  moneyNameCell = row.getCell(6);
//                    Cell displayNameCell = row.getCell(7); // 第7列：显示名称
//                    Cell descriptionCell = row.getCell(8); // 第8列：描述

                    // 获取显示名称和描述的值
//                    String displayName = getCellValueAsString(displayNameCell);
//                    String description = getCellValueAsString(descriptionCell);
                    String productId = getCellValueAsString(productIdCell);
                    String money = getCellValueAsString(moneyNameCell);
                    // 拼接字符串
//                    String result = "en_GB|" + displayName + "|" + description;
//                    String result = "AR;" + money;
                    String result = productId + "," + money + "0" + ",USD" ;


                    // 将结果添加到列表中
                    concatenatedResults.add(result);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return concatenatedResults;
    }

    private static String getCellValueAsString(Cell cell) {
        if (cell == null) {
            return "";
        }

        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString(); // 处理日期类型单元格
                } else {
                    return String.valueOf(cell.getNumericCellValue());
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            case BLANK:
                return "";
            default:
                return "";
        }
    }
}
